package com.wzbc.video.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wzbc.video.api.CommonResult;
import com.wzbc.video.entity.Lesson;
import com.wzbc.video.req.ChangeStatusReq;
import com.wzbc.video.req.IdReq;
import com.wzbc.video.req.SearchReq;
import com.wzbc.video.req.UserSearchReq;
import com.wzbc.video.service.LessonService;
import com.wzbc.video.utils.TokenUtil;
import nonapi.io.github.classgraph.json.Id;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/lesson")
public class LessonController {
    @Autowired
    private LessonService lessonService;

    @Autowired
    private TokenUtil tokenUtil;

    @PostMapping("/getLessons")
    public CommonResult getLessons(@RequestBody SearchReq req){
        return CommonResult.success(lessonService.listLesson(req));
    }

    @PostMapping("/getLesson")
    public CommonResult getLesson(@RequestBody IdReq req){
        return CommonResult.success(lessonService.getOneById(req.getId()));
    }

    @PostMapping("/insert")
    public CommonResult insertLesson(@RequestBody Lesson lesson, HttpServletRequest req){
        lesson.setIsShow(1);
        lesson.setUserId(Long.parseLong(tokenUtil.getToken(req.getHeader("X-Token"))));
        return CommonResult.success(lessonService.insert(lesson));
    }

    @PostMapping("/edit")
    public CommonResult editLesson(@RequestBody Lesson lesson){
        return CommonResult.success(lessonService.updateById(lesson));
    }

    @PostMapping("/change")
    public CommonResult changeLessonShow(@RequestBody ChangeStatusReq req){
        Lesson lesson = new Lesson();
        BeanUtils.copyProperties(req,lesson);
        return CommonResult.success(lessonService.update(new LambdaUpdateWrapper<Lesson>().eq(Lesson::getLessonId,req.getId()).set(Lesson::getIsShow,req.getStatus())));
    }
}

